/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package net.wofly.sale.order.repository;

import net.wofly.sale.order.domain.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SaleOrderRepository
        extends PagingAndSortingRepository<Order, String>, JpaSpecificationExecutor<Order> {
    
    @Override
    Page<Order> findAll(Specification <Order> specification, Pageable pageable);
    
    List<Order> findByPayTimeIsNullAndPayDeadlineLessThan(Long payDeadline);
    
    List<Order> findByOrderTypeAndStatusAndPayDeadlineLessThan(Integer orderType, Integer status, Long payDeadline);
    
    List<Order> findByPlaceOrderTypeAndStatusAndOrderTypeNotAndPayDeadlineLessThan(Integer placeOrderType, Integer status, Integer orderType,  Long payDeadline);
    
    public List<Order> findByProcessInsIDIn(List<String> processInsIDs) ;
    
    public Order findByProcessInsID(String processInsID) ;
    
    @Query(value = "select SUBSTR(max(orderno) ,9,12 ) as orderNo from sale_order_order where orderno like :dateStr% ",nativeQuery = true)
    public Object findMaxOrderNoByDate(@Param(value = "dateStr") String dateStr);


    List<Order> findByBuyerAndOrderCommitTimeBetween(String userID, Long startTime, Long endTime);

    List<Order> findByBuyerAndFinishedTimeBetween(String userID, Long startTime, Long endTime);

    List<Order> findByBuyerAndPayTimeBetween(String userID, Long startTime, Long endTime);
}